<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;

class OrgController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('check.login');
    }

    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.organization.list',compact('menu_id','toolBarButton'));
    }

    //获取组织列表
    public function getOrgList(){
        try{
            $org_list = callApiServer('System.OrgService.getOrgList',[]);
            return response()->json($org_list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }

    }

    //获取组织树
    public function getOrgTree(){
        try{
            $org_list = callApiServer('System.OrgService.getOrgTree',[]);
            return response()->json($org_list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }

    }

    public function showAddForm()
    {
        return view('system.organization.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('id');
        try{
            $info = callApiServer("System.OrgService.getOrgOne", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.organization.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('doc_name');
        $params['parent_id'] = request('name');
        $params['leader'] = request('leader');
        $params['remarks'] = request('remarks');

        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'组织名称不能为空']);
        }

        $rs = callApiServer("System.OrgService.saveOrgInfo", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function remove()
    {
        $params['id'] = request('id');
        $rs = callApiServer("System.OrgService.remove", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }
}
